package com.uflower.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import com.uflower.product.entity.CategoryEntity;
import com.uflower.product.service.CategoryService;
import com.uflower.common.utils.PageUtils;
import com.uflower.common.utils.R;



/**
 * 商品三级分类
 *
 * @author bridge
 * @email bridgezrq@163.com
 * @date 2022-01-15 14:52:35
 */
@RestController
@RequestMapping("product/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 列表 查询所有的分类及子分类  封装成一个树形结构
     */
    @RequestMapping("/list/tree")
    //  @RequiresPermissions("product:category:list")
    public R list(@RequestParam Map<String, Object> params){
        // 分页查询分类数据
        // PageUtils page = categoryService.queryPage(params);
        List<CategoryEntity> categoryList = categoryService.listWithTree();
        return R.ok().put("data", categoryList);
    }
    @ResponseBody
    @GetMapping({"categorys"})
    public List<CategoryEntity> getCategorysOnPage(){
        List<CategoryEntity> categorys = categoryService.getLevel1Categorys();
        return categorys;
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{catId}")
    //@RequiresPermissions("product:category:info")
    public R info(@PathVariable("catId") Long catId){
		CategoryEntity category = categoryService.getById(catId);

        return R.ok().put("category", category);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:category:save")
    public R save(@RequestBody CategoryEntity category){
		categoryService.save(category);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:category:update")
    public R update(@RequestBody CategoryEntity category){
		categoryService.updateCascade(category);

        return R.ok();
    }

    @RequestMapping("/update/sort")
    //@RequiresPermissions("product:category:update")
    public R updateSort(@RequestBody CategoryEntity[] category){
        categoryService.updateBatchById(Arrays.asList(category));

        return R.ok();
    }
    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:category:delete")
    public R delete(@RequestBody Long[] catIds){
		//categoryService.removeByIds(Arrays.asList(catIds));
        categoryService.removeCategoryByIds(Arrays.asList(catIds));
        return R.ok();
    }

}
